<?php
namespace app\models\system;

use app\admin\model\BaseModel;
use app\admin\model\ModelTrait;

class SystemGroupData extends BaseModel
{
    use ModelTrait;

    /**
     * 获取单个值
     * @param $config_name
     * @param int $limit
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getAllValue ($config_name,$limit = 0) {
        $model = self::alias('a')->field('a.*,b.config_name')->join('system_group b','a.gid = b.id')
            ->where("b.config_name",$config_name)->where("a.status",1)
            ->order('sort asc,id ASC');
        if($limit > 0) $model->limit($limit);
        $data = [];
        $result = $model->select();
        if(!$result) return $data;
        foreach ($result as $key => $value) {
            $data[$key]["id"] = $value["id"];
            $fields = json_decode($value["value"],true);
            foreach ($fields as $index => $field) {
//                $data[$key][$index] = $field['type'] == 'upload' ? (isset($field["value"][0]) ? $field["value"][0]: ''):$field["value"];
                $data[$key][$index] = $field["value"];
            }
        }
        return $data;
    }
    
    public static function getAllValueAdmin ($config_name,$limit = 0) {
        $model = self::alias('a')->field('a.*,b.config_name')->join('system_group b','a.gid = b.id')
            ->where("b.config_name",$config_name)->where("a.status",1)
            ->order('sort asc,id ASC');
        if($limit > 0) $model->limit($limit);
        $data = [];
        $result = $model->select();
        if(!$result) return $data;
        foreach ($result as $key => $value) {
            $fields = json_decode($value["value"],true);
            foreach ($fields as $index => $field) {
//                $data[$key][$index] = $field['type'] == 'upload' ? (isset($field["value"][0]) ? $field["value"][0]: ''):$field["value"];
                $data[$key][$index] = $field["value"];
            }
        }
        return $data;
    }
    public static function getAdminValue ($config_name,$valueName) {
        $model = self::alias("a")->field('a.*,b.config_name')->join('system_group b','a.gid = b.id')
        ->where("b.config_name",$config_name)->where("a.status",1)
        ->order("sort asc,id ASC");
        $data = [];
        $LabelValue = "优惠劵";
        $result = $model ->select();
        if(!$result) return $LabelValue;
        foreach ($result as $key => $value) {
            $fields = json_decode($value["value"],true);
            foreach ($fields as $index => $field) {
                $data[$key][$index] = $field["value"];
            }
        }
        foreach ($data as $v) {
            if ($v['value'] == $valueName) {
                $LabelValue = $v['label'];
            }
        }
        return $LabelValue;
    }
    
    //根据type 获取value
    public static function getValues ($config_name,$valueName) {
         $model = self::alias('a')->field('a.*,b.config_name')->join('system_group b','a.gid = b.id')
            ->where("b.config_name",$config_name)->where("a.status",1)
            ->order('sort asc,id ASC');
        $data = [];
        $result = $model->select();
        if(!$result) return $data;
        foreach ($result as $key => $value) {
            $data[$key]["id"] = $value["id"];
            $fields = json_decode($value["value"],true);
            foreach ($fields as $index => $field) {
                $data[$key][$index] = $field["value"];
            }
        }
        
        $list = [];
        
        foreach ($data as $v) {
            if($v['type'] == $valueName){
                $list = $v;
            }
        }
        
        
        return $list;
    } 
    
    
    //
    public static function getValueShop ($config_name,$thisValue) {
        $model = self::alias('a')->field('a.*,b.config_name')->join('system_group b','a.gid = b.id')
            ->where("b.config_name",$config_name)->where("a.status",1)
            ->order('sort asc,id ASC');
        $data = [];
        $result = $model->select();
        if(!$result) return $data;
        foreach ($result as $key => $value) {
            $data[$key]["id"] = $value["id"];
            $fields = json_decode($value["value"],true);
            foreach ($fields as $index => $field) {
//                $data[$key][$index] = $field['type'] == 'upload' ? (isset($field["value"][0]) ? $field["value"][0]: ''):$field["value"];
                $data[$key][$index] = $field["value"];
            }
        }
        
        $name = "";
        foreach($data as $v){
            if($v['value'] == $thisValue){
                $name = $v['label'];
            }
        }
        return $name;
    }
    //获取一个店铺的列表
    public static function getValueShopList($config_name,$thisValue){
        $model = self::alias('a')->field('a.*,b.config_name')->join('system_group b','a.gid = b.id')
            ->where("b.config_name",$config_name)->where("a.status",1)
            ->order('sort asc,id ASC');
        $data = [];
        $result = $model->select();
        if(!$result) return $data;
        foreach ($result as $key => $value) {
            $data[$key]["id"] = $value["id"];
            $fields = json_decode($value["value"],true);
            foreach ($fields as $index => $field) {
//                $data[$key][$index] = $field['type'] == 'upload' ? (isset($field["value"][0]) ? $field["value"][0]: ''):$field["value"];
                $data[$key][$index] = $field["value"];
            }
        }
        
        $name = [];
        foreach($data as $v){
            if($v['value'] == $thisValue){
                $name = $v;
            }
        }
        return $name;
    }
}
